<?php
namespace app\admin\model;

use think\Model;


class Category extends Model
{
	protected $autoWriteTimestamp = true;
	protected $updateTime = false;

	protected $auto = ['status'];
	public function archive()
	{
		return $this->hasMany('Archive');
	}

	public function article()
	{
		//
	}

	public function deleteCategory($id)
	{
		$categoryRow = self::get($id);

		if($categoryRow == false)
		{
			$this->error = "this category [id:". $id ."] do not exist";

			return false;
		}

		//删除attach的文档

		//删除附属的图片,文章等等等等
		//Strings::deleteFile($aricleRow->thumbnail);

		//删除掉分类
		return $categoryRow->delete();
	}

	public static function selectField()
	{
		return self::field('id,title,pid');
	}

	public function scopeTree($query)
	{
		$query->where('status',1)->field('id,title,pid');
	}
	public function getCateTree()
	{
		//获取 cateData 以便于分类
		$data = \app\common\tools\BinTools::collectToTree(
			$this->scope('tree')->select()->toArray()
			);
		return $data;
	}

    protected function setStatusAttr($value)
    {
    	$maps = ['on'=>'1','off'=>'0'];
    	if(array_key_exists($value, $maps))
    	{
    		return $maps[$value];
    	}elseif(is_null($value))
    	{
    		//关闭时默认为Null
    		return 0;
    	}
        return $value;
    }

	protected function genTree5($items) { 
	    foreach ($items as $item) 
	        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; 
	    return isset($items[0]['son']) ? $items[0]['son'] : array(); 
	}

}